"""
    这个文件是把ｐｓ里导出来的ｍａｓｋ的ｐｎｇ图片，进行一个二值化变成黑白的过程。
"""
from config import get_settings
import os
from PIL import Image

settings = get_settings()
def binarize_image(image_path, threshold, image_name):
    # 打开图片
    image = Image.open(image_path)
    
    # 将图片转换为灰度图像
    gray_image = image.convert('L')
    
    # 创建一个新的二值化图像
    binary_image = gray_image.point(lambda x: 0 if x < threshold else 255, '1')
    
    # 保存二值化图像
    binary_image.save(image_name)

# 指定图片路径和阈值
image_path = os.path.join(settings.BASE_DIR, "points.png")


threshold = 50


# 运行二值化函数
def direction_mask():
    binarize_image(image_path, threshold, os.path.join(settings.BASE_DIR, "static", "direction_mask.png"))

def greed_point_mask():
    binarize_image(image_path, threshold, os.path.join(settings.BASE_DIR, "static", "green_point_mask.png"))


if __name__ == "__main__":
    # direction_mask() # 看情况使用
    greed_point_mask()

